{%- if storeConfig.gtm_pixel_config -%}
  {% assign google_gtm_ids = storeConfig.gtm_pixel_config | strtoarray %}
  {% get_pixels_domain list={google_gtm_ids} %}
  {% if list %}
    <!-- Google Tag Manager -->
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','{{list[0].pixel_id}}');</script>
    <!-- End Google Tag Manager -->
    <script>
      console.log(storeConfig.gtm_pixel_config, {{list | json}});
      document.addEventListener('DOMContentLoaded', function () {
        function pushGoogleGtm(event,params = {}) {
          dataLayer.push({ ecommerce: null }); 
          if(event === 'front_page_view'){
            dataLayer.push({ event: event,...params });
          }else{
            dataLayer.push({ event: event,ecommerce: params }); 
          }
        }

        {% if template == 'search_detail' and request_param.page == 1 %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:'{{search.keywords | filter_string_format}}',
          front_page_id:""
        });


        {% elsif template == 'module_notfound' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"exception",
          front_page_id:"not_found"
        });

        {% elsif template == 'module_expired' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"exception",
          front_page_id:"store_expired"
        });

        {% elsif template == 'module_unavaiable' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"exception",
          front_page_id:"store_unavaiable"
        });

        {% elsif template == 'module_checkout_limit' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"exception",
          front_page_id:"store_checkout_limit"
        });

        {% elsif template == 'module_login' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"exception",
          front_page_id:"firewall"
        });

        {% elsif template == 'module_freedomain_login' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"exception",
          front_page_id:"freedomain_login"
        });


        {% elsif template == 'module_message' and page_ca == 'order_callback' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"exception",
          front_page_id:"order_callback"
        });

        {% elsif template == 'module_message' and page_ca == 'order_checkout' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"exception",
          front_page_id:"checkout"
        });

        {% elsif template == 'product_detail' or template == 'product_quick_detail' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:_GET_C_SETTING_('product')['title'],
          front_page_id:_GET_C_SETTING_('product')['id']
        });
        {% elsif template == 'collection_detail' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:_GET_C_SETTING_('collection')['title'],
          front_page_id:_GET_C_SETTING_('collection')['id']
        });
        {% elsif template == 'blog_detail' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:_GET_C_SETTING_('blog')['title'],
          front_page_id:_GET_C_SETTING_('blog')['id']
        });

        {% elsif template == 'page_detail' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:_GET_C_SETTING_('page')['title'],
          front_page_id:_GET_C_SETTING_('page')['id']
        });

        {% elsif template == 'topic_detail' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"{{topic.title}}",
          front_page_id:"{{topic.id}}"
        });
        {% elsif template == 'blog_collection' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"news",
          front_page_id:""
        });

        {% elsif template == 'blog_collection_detail' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"{{news_detail.title}}",
          front_page_id:"{{news_detail.id}}"
        });

        {% elsif template == 'promotion_detail' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"{{promotion.promotion_name}}",
          front_page_id:"{{promotion.id}}"
        });

        {% elsif template == 'coupon_detail' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_name:"{{coupon.coupon_name}}",
          front_page_id:"{{coupon.id}}"
        });
        {% elsif template == 'order_success' %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
          front_page_id:"{{order.checkout_token}}",
        });
        {% else %}
        pushGoogleGtm('front_page_view', {
          front_page_ca:_GET_C_SETTING_('page_ca'),
        });
        {% endif %}

        window['TOP_GLOBAL_CALLBACK_LAYER'].register({
          view_content: function () {
            pushGoogleGtm('view_item',{
                currency:_GET_C_SETTING_("base_currency").standard_code,    
                value:_GET_C_SETTING_('product').variant.price,
                items:[
                    {
                        item_id:_GET_C_SETTING_('product').spu,
                        item_name: _GET_C_SETTING_('product').title,
                        price: _GET_C_SETTING_('product').variant.price,
                    }
                ]
            });
          },

          search: function () {
            pushGoogleGtm('Search',{
              search_term: '{{search.keywords | filter_string_format}}',
              items: _GET_C_SETTING_('search_products').map(item => ({
                id: item.spu,
                name: item.title
              }))
            });
          },
          purchase: function () {
                {%- if controller == 'order' and action == 'success' -%}
                {% capture pixels %}reddit_pixels_{{order.id}}{% endcapture %}
                {% get_cache key={pixels} var_name=reddit_pixels_status ttl=2592000 %}
                {% if reddit_pixels_status == 0 %}
                pushGoogleGtm('purchase',{
                currency: _GET_C_SETTING_('order').currency_code,
                {% if storeConfig.gtm_sales_goods_amount %}
                value: "{{ order.current_subtotal_price| plus: order.current_coupon_price| plus: order.current_promotion_price }}",
                {% else %}
                value: "{{ order.total_price }}",
                {% endif %}
                transaction_id: _GET_C_SETTING_('order').order_number,
                items: _GET_C_SETTING_('order').products.map(item => ({
                    item_id: item.spu,
                    item_name: item.quantity,
                    price: item.price
                })),
                });
                {% endif %}
                {% endif %}
          },   

          add_to_wishlist: function () {
            pushGoogleGtm('add_to_wishlist');
          },

          add_to_cart: function (ecdata) {
            pushGoogleGtm('add_to_cart',{
              currency: _GET_C_SETTING_('base_currency_code'),
              value: ecdata['ec_data_price'] * ecdata['ec_data_quantity'],
              items: [{
                item_id: ecdata['ec_data_spu'],
                item_name: ecdata['ec_data_name'],
                quantity: ecdata['ec_data_quantity'],
                price: ecdata['ec_data_price']
              }]
            });
          },

          sign_up: function () {
            pushGoogleGtm('sign_up');
          },
          login: function () {
            pushGoogleGtm('login');
          },
          begin_checkout: function () {
            pushGoogleGtm('begin_checkout',{
                currency:_GET_C_SETTING_("base_currency").standard_code,    
                value:_GET_C_SETTING_('cart').total_price,    
                items:_GET_C_SETTING_('cart').items.map(item => ({
                    item_id:item.product.spu,
                    item_name: item.variant.title,
                    price: item.variant.price,
                    quantity: item.quantity
                }))
            });
          },
          add_shipping_information: function (params) {
            pushGoogleGtm('add_shipping_info',{
                currency:_GET_C_SETTING_("base_currency").standard_code,    
                value:_GET_C_SETTING_('cart').total_price,    
                items:_GET_C_SETTING_('cart').items.map(item => ({
                    item_id:item.product.spu,
                    item_name: item.variant.title,
                    price: item.variant.price,
                    quantity: item.quantity
                }))
            });
          },
          add_payment_information: function (params) {
            pushGoogleGtm('add_payment_info',{
                currency:_GET_C_SETTING_("base_currency").standard_code,    
                value:_GET_C_SETTING_('cart').total_price,
                {% if storeConfig.gtm_contact == "1" %}
                {% if template == "orderonepage_index" %}
                email:getPageParams().order_info.customer_email,
                phone:getPageParams().shipping_address.phone,
                {% else %}
                email:'{% if pixel_user.email %}{{pixel_user.email }}{% endif %}',    
                phone:'{% if pixel_user.phone_number %}{{pixel_user.phone_number }}{% endif %}',    
                {% endif %}    
                {% endif %}

                items:_GET_C_SETTING_('cart').items.map(item => ({
                    item_id:item.product.spu,
                    item_name: item.variant.title,
                    price: item.variant.price,
                    quantity: item.quantity
                }))
            });
          },
          share_product: function (params) {
            pushGoogleGtm('share',{
              content_type: params.ec_data_image,
              item_id:params.ec_data_spu
            });
          },
          remove_from_cart:function(params){
            pushGoogleGtm('remove_from_cart',{
                currency:_GET_C_SETTING_("base_currency").standard_code,    
                value:params.price * params.quantity,
                items:[
                    {
                        item_id:params.ec_data_spu,
                        item_name: params.ec_data_name,
                        price: params.ec_data_price,
                        quantity: params.ec_data_quantity
                    }
                ]
            });
          }
        });
      });
    </script>

  {% endif %}
{%- endif -%}
